package com.xl.ui_demo_widget;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;

import com.xl.ui_demo_widget.adapter.ClassroomBuildingAdapter;
import com.xl.ui_demo_widget.pojo.ClassroomBuilding;

import java.util.ArrayList;

public class SpinnerActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_spinner);

        //数据（data）<-->数据适配器（adapter）<-->界面（spinner）
        //region 学期下拉框：spinner加载静态数据
        Spinner spsemester = findViewById(R.id.spinner);
        spsemester.setSelection(0,true);
        spsemester.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position==0) return;
                Toast.makeText(SpinnerActivity.this,parent.getItemAtPosition(position).toString(),Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //endregion

        //region 学院下拉框：spinner通过ArrayAdapter加载动态数据,使用系统预定义下拉列表视图
        String[] school={"==选择学院==","计算机学院","理学院","外语学院"};
        Spinner spShool = findViewById(R.id.sp_shool);
        spShool.setSelection(0,true);
        ArrayAdapter<String> shoolAdapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, school);

        spShool.setAdapter(shoolAdapter);

        spShool.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position==0) return;
                Toast.makeText(SpinnerActivity.this,parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //endregion

        //region 年级下拉框：spinner通过ArrayAdapter加载动态数据，使用自定义下拉列表视图
        String[] grades = {"-==选择年级==-", "大一", "大二", "大三", "大四"};
        Spinner spGrades = findViewById(R.id.sp_grade);
        ArrayAdapter<String> gradeAdapter = new ArrayAdapter<>(this,R.layout.my_spinner_item,grades);

        gradeAdapter.setDropDownViewResource(R.layout.my_spiner_item_dropdown);
        spGrades.setAdapter(gradeAdapter);
        spGrades.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position==0) return;
                Toast.makeText(SpinnerActivity.this,parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });

        //endregion

        //region 教学楼下拉框：spinner通过自定义适配器加载动态数据，使用自定义下拉列表视图
        ArrayList<ClassroomBuilding> classroomBuilding = new ArrayList<>();
        classroomBuilding.add(new ClassroomBuilding("-==选择教室==-","",""));
        classroomBuilding.add(new ClassroomBuilding("文友楼","510","多媒体"));
        classroomBuilding.add(new ClassroomBuilding("实验楼","234","计算机"));
        classroomBuilding.add(new ClassroomBuilding("香园楼","453","数学"));
        classroomBuilding.add(new ClassroomBuilding("香园楼","453","数学"));
        Spinner spClassroomBuilding = findViewById(R.id.sp_classroom_building);
        spClassroomBuilding.setAdapter(new ClassroomBuildingAdapter(this,classroomBuilding));
        spClassroomBuilding.setSelection(0,true);
        spClassroomBuilding.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener(){

            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                if(position==0) return;
                Toast.makeText(SpinnerActivity.this,parent.getItemAtPosition(position).toString(),
                        Toast.LENGTH_SHORT).show();
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        //endregion

    }

}